package com.jlz.actions.console.valuetype;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Standard;
import org.springframework.beans.factory.annotation.Autowired;

import com.jlz.beans.def.ValuetypeBean;
import com.jlz.managers.def.Valuetypes;
import com.julewa.Client;

@Standard(rest="valuetypeId")
public class IndexAction {
	
	@Autowired Client client;
	Integer valuetypeId = null;
	@Autowired Valuetypes valuetypes;
	ValuetypeBean valuetype; 
	
	public String execute(){
		if(valuetypeId != null){
			valuetype = valuetypes.get(valuetypeId);
		}else{
			List<ValuetypeBean> ls = valuetypes.getAll();
			if(ls == null || ls.isEmpty()){
				valuetype = valuetypes.create();
			}else{
				valuetype = ls.get(0);
			}
		}
		return "edit";
	}

	@Action("remove")
	public String delete(){
		if(valuetypeId != null){
			valuetype = valuetypes.get(valuetypeId);
			if(valuetype != null){
				valuetypes.delete(valuetype);
			}
			valuetypeId = null;
		}
		return execute();
	}
	
	public ValuetypeBean getValuetype(){
		return valuetype;
	}

	public Integer getValuetypeId() {
		return valuetypeId;
	}

	public void setValuetypeId(Integer valuetypeId) {
		this.valuetypeId = valuetypeId;
	}
}
